#ifndef __AT32F421_BSP_H__
#define __AT32F421_BSP_H__

#include "at32f421.h"
#include <stdbool.h>

typedef struct{
    gpio_type *ptPort;
    uint16_t hwPin;
}io_t;

typedef struct{
    uint8_t chSource;
    uint8_t chMux;
}gmux_t;
typedef struct{
    uint32_t wPin;
    gpio_type *ptPort;
    
    gmux_t tGmux;
}ggpio_t;

typedef struct{
    ggpio_t tInA;
    ggpio_t tInB;
    ggpio_t tInC;
    ggpio_t tInD;
}stepmotor_t;

// A:which
// B:status
#define TRIGGER(A,B)        gpio_bits_write(c_tTrigger[A].ptPort, c_tTrigger[A].wPin, B)
#define INCLUSIVE(A,B)      gpio_bits_write(c_tInclusive[A].ptPort, c_tInclusive[A].wPin, B)

#define STEPMOTOR_A(A,B)    gpio_bits_write(c_tStepMotor[A].tInA.ptPort, c_tStepMotor[A].tInA.wPin, B)
#define STEPMOTOR_B(A,B)    gpio_bits_write(c_tStepMotor[A].tInB.ptPort, c_tStepMotor[A].tInB.wPin, B)
#define STEPMOTOR_C(A,B)    gpio_bits_write(c_tStepMotor[A].tInC.ptPort, c_tStepMotor[A].tInC.wPin, B)
#define STEPMOTOR_D(A,B)    gpio_bits_write(c_tStepMotor[A].tInD.ptPort, c_tStepMotor[A].tInD.wPin, B)

void system_clock_config(void);
void bsp_Init(void);

void bsp_LedSet(bool bStatus);
uint16_t usart_sendData(uint8_t chUsartNum, uint8_t *pchSendData, uint16_t hwLength);
uint16_t usart_receiveData(uint8_t chUsartNum, uint8_t *pchReceiveData);
void USART1_TimeOutCounter(void);
void USART2_TimeOutCounter(void);

void bsp_StepMotor(uint8_t chStepMotorNum, uint8_t chInputData);
#endif